import {
    LOGIN_SUCCESS,
    USERINFO_SUCCESS,
    USERINFO_FAILURE,
    LOGOUT_USER,
    UPDATE_USER_SUCCESS,
    CHANGE_COMMUNITY,
    SET_TRUENAME,
    SET_PHONE,
    SET_WECHATID
} from '../types'

let token= localStorage.token || null;


const state = {
    token: token,
    user: null,
    openid:null,
    wx:null,
    wechatId:localStorage.wechatId ? localStorage.wechatId : null,
    pageParams:{
      pageNum:1,
      pageLimit:12,
      where:{wechatId: localStorage.wechatId ? localStorage.wechatId: null}
    }
};

const mutations = {
    [LOGIN_SUCCESS](state , action){
        localStorage.setItem("token",action.token);
        state.token = action.token
    },
    [USERINFO_SUCCESS](state,action){
        state.user = action.user;
    },
    [USERINFO_FAILURE](state,action){
        state.user = null
    },
    [LOGOUT_USER](state,action){
        localStorage.removeItem("token");
        state.user = null;
        state.token = null;
    },
    [UPDATE_USER_SUCCESS](state,action){
        state.user = action.user
    },
    [CHANGE_COMMUNITY](state,action){
        state.user.community_id = action.id
    },
    [SET_TRUENAME](state,action){
        state.user.truename = action.truename
    },
    [SET_PHONE](state,action){
        state.user.phone = action.phone
    },
    [SET_WECHATID](state,action){
      state.wechatId = action.wechatId;
      state.pageParams.where.wechatId=state.wechatId;
      localStorage.setItem("wechatId",action.wechatId);
    }
};



export default {
    state,
    mutations
}
